package com.weixing.mall.provider.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.annotation.NoRepeatSubmit;
import com.weixing.mall.base.enums.WhetherEnum;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.core.support.auth.MemAuth;
import com.weixing.mall.core.support.auth.MemInfo;
import com.weixing.mall.provider.api.model.vo.MemberRpcVo;
import com.weixing.mall.provider.api.service.MemberServiceRpc;
import com.weixing.mall.provider.model.domain.AgentApply;
import com.weixing.mall.provider.model.dto.ApplyForAgentDto;
import com.weixing.mall.provider.service.IAgentApplyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author kfc
 * @Title: ${FrontAgentController}
 * @Package ${package_name}
 * @Description: ${todo}
 * @date 2019/11/115:40
 */
@RestController
@RequestMapping("front/agent")
@Validated
@Api(value = "前端:代理商", tags = "前端:会员申请成为代理商")
public class ApplyForAgentController extends BaseController {
    @Autowired
    private IAgentApplyService agentApplyService;
    @Autowired
    private MemberServiceRpc memberServiceRpc;


    @ApiOperation(value = "申请成为代理商")
    @PostMapping("applyForAgent")
    @NoRepeatSubmit
    public Result applyForAgent(@MemInfo MemAuth memAuth,@RequestBody ApplyForAgentDto applyForAgentDto) {
        agentApplyService.apply(applyForAgentDto, memAuth);
        return ResultUtil.success();
    }


    @ApiOperation(value = "申请成为代理商前判断")
    @PostMapping("applyCheck")
    @NoRepeatSubmit
    public Result applyCheck(@MemInfo MemAuth memAuth) {
        /*
         * 如果已经是代理商就不让申请
         * 如果已经申请过，则给予响应提示
         */
        Result<MemberRpcVo> memberRpcVoResult = memberServiceRpc.selectById(memAuth.getMid());
        if (!memberRpcVoResult.isSuccess()) {
            return ResultUtil.success("服务暂不可用，请稍后重试！");
        }
        MemberRpcVo data = memberRpcVoResult.getData();
        if (WhetherEnum.YES.getValue().equals(data.getIsAgent())) {
            return ResultUtil.error("您已经是代理商了，不能够重复申请");
        }

        QueryWrapper<AgentApply> wrapper = new QueryWrapper<AgentApply>().eq("member_id", memAuth.getMid());
        AgentApply one = agentApplyService.getOne(wrapper);
        if (null != one) {
            return ResultUtil.error("您已提交过申请，正在等待客服处理");
        }
        return ResultUtil.success();
    }
}
